# !/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2020/12/17 21:36
# @Author  : darknight
# @File    : parser.py
# @Software: PyCharm

from core.parser import BaseParse



class Parser(BaseParse):
    @staticmethod
    def parser_recommend_songs_list(source, result) -> list:
        data = [dict(
            id=item['listId'],
            title=item['collectName'],
            pic=item['collectLogo'],
            listen_num=item['playCount'],
            source=source,
        ) for item in result['result']['data']['collects']]
        return data[0: Parser.LIMIT]

    @staticmethod
    def parser_recommend_songs(source, result) -> list:
        data = [dict(
            id=item['songId'],
            mid=item['songStringId'],
            songName=item['songName'],
            singer=[item['singers']],
            vid=item['mvId'],
            pic=item['albumLogo'],
            source=source,
        ) for item in result['result']['data']['songs']]
        return data[0:Parser.LIMIT]

    @staticmethod
    def parser_songs_list_by_category(source, result) -> list:
        result = result['result']['data']['collects']
        data = [dict(
            id=item['listId'],
            pic=item['collectLogo'],
            listen_num=item['playCount'],
            title=item['collectName'],
            source=source,
        ) for item in result]
        return data[0:Parser.LIMIT]

    @staticmethod
    def parser_songs_list_detail(source, result) -> dict:
        songs = result['result']['data']['collectSongs']
        detail = result['result']['data']['collectDetail']
        data = dict(
            id=detail['listId'],
            logo=detail['collectLogo'],
            title=detail['collectName'],
            desc=detail['description'],
            listen_num=detail['playCount'],
            creator=detail['userName'],
            source=source,
            songs=[
                dict(
                    id=song['songId'],
                    mid=song['songStringId'],
                    songName=song['songName'],
                    singer=[song['singers']],
                    vid=song['mvId'],
                    pic=song['albumLogo'],
                    source=source,
                ) for song in songs
            ]
        )
        return data

    @staticmethod
    def parser_song_detail(source, result) -> dict:
        res = result['result']["data"]['songDetail']
        data = dict(
            id=res['songId'],
            mid=res['songStringId'],
            songName=res['songName'],
            singer=[res['singers']],
            vid=res['mvId'],
            pic=res['albumLogo'],
            duration=res['length'],
            source=source,
        )
        return data

    @staticmethod
    def parser_song_play_url(source, result) -> str:
        url_list = result['result']['data']['songPlayInfos'][0]['playInfos']
        data = list(filter(lambda x: x['listenFile'], url_list))[0]['listenFile']
        return data

    @staticmethod
    def parser_song_lyric(source, result) -> str:
        lyr_list = result['result']['data']['lyrics']
        data = list(filter(lambda x: x['type'] == 2, lyr_list))[0]['content']
        return data

    @staticmethod
    def parser_mv_play_url(source, result) -> list:
        _q = {
            "ld": "标清",
            "sd": "高清",
            "hd": "超清",
            "fhd": "1080p",
        }
        if isinstance(result, str):
            data = [{"高清": result}]
        else:
            data = list(map(lambda x: {_q[x.split('/')[-2]]: x}, result))
        return data

    @staticmethod
    def parser_top_list(source, result) -> dict:
        detail = result['result']['data']['billboard']
        songs = detail['songs']
        data = dict(
            id=detail['billboardId'],
            logo=detail['logo'],
            title=detail['name'],
            desc=detail['description'],
            listen_num=detail['updateDate'],
            source=source,
            songs=[dict(
                id=song['songId'],
                mid=song['songStringId'],
                songName=song['songName'],
                singer=[song['singers']],
                vid=song['mvId'],
                pic=song['albumLogo'],
                source=source,
            ) for song in songs],
        )
        return data

    @staticmethod
    def parser_search_song(source, result) -> list:
        songs = result['result']['data']['songs']
        data = [dict(
            id=song['songId'],
            mid=song['songStringId'],
            songName=song['songName'],
            singer=[song['singers']],
            vid=song['mvId'],
            pic=song['albumLogo'],
            source=source,
        ) for song in songs]
        return data[0: Parser.LIMIT]
